package net.organizer.ui.actionhandler;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import net.organizer.dao.Domain;
import net.organizer.dao.Person;

import org.json.JSONObject;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class PersonPutCreateActionHandler extends ActionHandlerBase {
	private static final Logger logger = Logger.getLogger(PersonPutCreateActionHandler.class.getName());
	
	@Override
	public void handleAction(HttpServletRequest req, JSONObject respJSONObject) throws Exception {
		logger.fine("handleAction");
		Objectify ofy = ObjectifyService.beginTransaction();
		
		Domain domain = new Domain();
		domain.setName("domain1");
		try{
			Person person = new Person();
			person.setDomainKey(domain.getKey());
			person.setGoogleUserId("new" + System.currentTimeMillis());
			person.setFirstName("new");
			person.setLastName("new");
			person.setDateOfBirth(new Long(System.currentTimeMillis()));
			logger.fine(String.format("person after applying http params: %s", person));
			ofy.put(person); // figures out key from annotations?
			ofy.getTxn().commit();
			respJSONObject.put("person", person.toJSONObject());
		}finally{
			if(ofy.getTxn().isActive()){
				ofy.getTxn().rollback();
			}
		}
	}
}
